package practise.alogrithm.hundredInterviewQuestions;

import practise.alogrithm.hundredInterviewQuestions.Utils.LinkedListBuilder;
import practise.alogrithm.hundredInterviewQuestions.Utils.ListNode;

/**
 * Created by richard on 6/2/14.
 */
public class GetLastElementInLink {
    public static ListNode getLastKNode(ListNode head,int k){
        ListNode slowNode = head;
        ListNode fastNode = head;
        for(int i=0;i<k;i++){
            fastNode= fastNode.getNext();
            if(fastNode.getNext()==null){
                return null;
            }

        }
        while(fastNode.getNext()!=null){
            slowNode=slowNode.getNext();
            fastNode=fastNode.getNext();
        }
        return slowNode;
    }

    public static void main(String[] args){
        System.out.println("result:"+getLastKNode(LinkedListBuilder.buildLinkedListFromArray(new int[]{1,2,3,4,5,6,7,8,9,10,11}),3).getValue());
    }
}
